/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;

/**
 * This class translate the learning objecttive element
 *
 */
public class LearningObjectivesTranslator {

    /**
     * Creates a new instance of LearningObjectivesTranslator    
     * @param lnode The learning objective node of the Dom structure
     * @param ont The ontology
     * @param instActivity The activity instance
     ** @param instLearningDes The learning design instance
     * @param nsName The namespace
     */
    public LearningObjectivesTranslator(Node lnode, OntModel ont,
            Individual instActivity, Individual instLearningDes, String nsName) {

        try {

            OntClass learningObjective =
                    ont.getOntClass(nsName + "Learning-Objective");


            NodeList children =
                    lnode.getChildNodes();
            String ldTitle =
                    "";
            lastInstanceSLDTranslator lastInst =
                    new lastInstanceSLDTranslator();

            String instance =
                    "learning_objective_instance_" +
                    (String.valueOf(lastInst.lastInstance(learningObjective)));
            Individual instLd =
                    ont.createIndividual(nsName + instance, learningObjective);

            if (children != null) {
                for (int i =
                        0; i < children.getLength(); i++) {
                    //Creates the instance                         



                    if (lnode.getParentNode().getNodeName().compareTo("imsld:learning-activity") ==
                            0) {
                        ObjectProperty learningObjectiveRef =
                                ont.getObjectProperty(nsName +
                                "learning-objective");
                        instActivity.setPropertyValue(learningObjectiveRef,
                                instLd);

                    }

                    if (lnode.getParentNode().getNodeName().compareTo("imsld:learning-design") ==
                            0) {
                        ObjectProperty learningObjectiveRef =
                                ont.getObjectProperty(nsName +
                                "learning-objective");
                        instLearningDes.setPropertyValue(learningObjectiveRef,
                                instLd);

                    }

                    if (children.item(i).getNodeName().compareTo("imsld:title") ==
                            0) {

                        DatatypeProperty title =
                                ont.getDatatypeProperty(nsName + "title");
                        RDFNode titleValue =
                                ont.createTypedLiteral(children.item(i).
                                getTextContent());
                        instLd.setPropertyValue(title, titleValue);

                    }
                    if (children.item(i).getNodeName().compareTo("imsld:item") ==
                            0) {
                        NamedNodeMap attrsItem =
                                children.item(i).getAttributes();
                        for (int x =
                                0; x < attrsItem.getLength(); x++) {
                            Node attr =
                                    attrsItem.item(x);
                            if (attr.getNodeName().trim().compareTo("identifier") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                DatatypeProperty identifier =
                                        ont.getDatatypeProperty(nsName +
                                        "identifier");
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode value =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLd.setPropertyValue(identifier, value);

                            }
                            if (attr.getNodeName().trim().compareTo("identifierref") ==
                                    0) {
                                DatatypeProperty identifierRef =
                                        ont.getDatatypeProperty(nsName +
                                        "identifier-ref");
                                RDFNode identifierRefValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLd.setPropertyValue(identifierRef,
                                        identifierRefValue);

                            }
                            if (attr.getNodeName().trim().compareTo("isvisible") ==
                                    0) {
                                DatatypeProperty visible =
                                        ont.getDatatypeProperty(nsName +
                                        "isvisible");
                                RDFNode visibleValue;

                                if (attr.getNodeValue().compareTo("true") == 0) {
                                    visibleValue =
                                            ont.createTypedLiteral(true);
                                } else {
                                    visibleValue =
                                            ont.createTypedLiteral(false);
                                }

                                instLd.setPropertyValue(visible, visibleValue);

                            }
                            if (attr.getNodeName().trim().compareTo("metadataLOMref") ==
                                    0) {
                                ObjectProperty metadataRef =
                                        ont.getObjectProperty(nsName +
                                        "metadata-ref");
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#Metadata");
                                RDFNode metadataValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLd.setPropertyValue(metadataRef,
                                        metadataValue);

                            }
                        }   //for   
                    }//if      
                }
            }//if children !=null     
        }//try
        catch (Exception ex) {
            ex.printStackTrace();
        }


    }
}
